package com.license.servlets;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.license.logic.pojo.User;
import com.license.presentation.identity.UserPresentation;
import com.license.presentation.recomandation.RecomandationOptimization;

public class UserServlet extends HttpServlet {

	private UserPresentation userPresentation;
	
	private RecomandationOptimization recomad;

	public UserServlet() {
		userPresentation = new UserPresentation();
		recomad = new RecomandationOptimization();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		
		HttpSession session = request.getSession(true);
		String opeString = request.getParameter("loginPageOperation");
		String path = "";
		try {
			if(opeString == null || "login".equals(opeString)){
				String userName = request.getParameter("name");
				String password = request.getParameter("password");
	
				User user = userPresentation.logIn(userName, password);
	
				if (user != null) {
	
					
					session.setAttribute("currentSessionUser", user);
					path = "../faces/JSFfiles/StartPage.jsp";
//					response.sendRedirect("../faces/JSFfiles/StartPage.jsp"); // logged-in page
//					return;
				}
	
				else{
					path = "../faces/JSFfiles/InvalidLoginPage.jsp";
//					response.sendRedirect("../faces/JSFfiles/InvalidLoginPage.jsp"); // error page
//					return;
				}
			} else if ("logout".equals(opeString)){
				System.out.println("Esti pe log off");
				session.setAttribute("currentSessionUser", null);
				path = "../faces/JSFfiles/LogInPage.jsp";
//				response.sendRedirect("../faces/JSFfiles/LogInPage.jsp");
//				return;
			}
		}
		catch (Exception theException) {
			theException.printStackTrace();
			System.out.println(theException);
//			return;
		}
		try {
		if (!"".equals(path)){
			response.sendRedirect(path);
		}
		} catch (Exception e){
			
		}
		return;
	}
	public static void logOut(HttpServletRequest request, HttpServletResponse response) {
		try{
			HttpSession session = request.getSession(true);
			session.setAttribute("currentSessionUser", null);
			response.sendRedirect("../faces/JSFfiles/LogOutPage.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public List<User> getUsers(HttpServletRequest request, HttpServletResponse response) {

		List<User> users = new ArrayList<User>();
		try{
		users = userPresentation.getUsers();
		} catch(Exception e){
			e.printStackTrace();
		}
		return users;
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		try {
			String userName = request.getParameter("userName");
			String operation = request.getParameter("operation");
			if(operation.equalsIgnoreCase("selectUser")){
				User selectedUser = selectUser(userName);
				request.getSession().setAttribute("userToEdit_userName", selectedUser.getUserName());
				//request.getSession().setAttribute("userToEdit", selectedUser);
			}
			User currentUser = (User)request.getSession().getAttribute("userToEdit");
			if(operation.equalsIgnoreCase("deleteUser")) {
				User user = new User();
				user.setUserName((String)request.getSession().getAttribute("userToEdit_userName"));
				deleteUser(user);
			} else if ("createUser".equals(operation)){
				User createdUser = new User();
				createdUser.setAddress(request.getParameter("address"));
				createdUser.setAuthorization(request.getParameter("authorization"));
				createdUser.setContact(request.getParameter("contact"));
				createdUser.setName(request.getParameter("name"));
				createdUser.setPassword(request.getParameter("password"));
				createdUser.setUserName(request.getParameter("createdUserName"));
				
				insertUser(createdUser);
				response.sendRedirect("../faces/JSFfiles/UsersPage.jsp");
				return;
			} else if ("changeToEditUserPage".equals(operation)){
				User selectedUser = selectUser(userName);
				request.getSession().setAttribute("userToEdit", selectedUser);
			} else if(operation.equalsIgnoreCase("editUser")) {
				User user = (User)request.getSession().getAttribute("userToEdit");
				
				String editedUserName = user.getUserName();
				String name  = request.getParameter("name");
				String contact  = request.getParameter("contact");
				String authorization  = request.getParameter("authorization");
				String password  = request.getParameter("password");
				String address = request.getParameter("address");
				if(password == null && currentUser != null) {
					password = currentUser.getPassword();
					user.setUserName(editedUserName);
					user.setName(name);
					user.setPassword(password);
					user.setAuthorization(authorization);
					user.setContact(contact);
					user.setAddress(address);
				} else if(password != null && currentUser != null) {
					user.setUserName(currentUser.getUserName());
					user.setName(currentUser.getName());
					user.setPassword(password);
					user.setAuthorization(currentUser.getAuthorization());
					user.setContact(currentUser.getContact());
					user.setAddress(currentUser.getAddress());
				}
			
				
			
				editUser(user);
				response.sendRedirect("../faces/JSFfiles/UsersPage.jsp");
				return;
			}
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void deleteUser(User user) {
		try {
			if(user.getUserName() != null && !user.getUserName().isEmpty())
			userPresentation.deleteUser(user);
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	private void editUser(User user) {
		try {
			if(user.getUserName() != null && !user.getUserName().isEmpty())
			userPresentation.editUser(user);
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void insertUser(User user) {
		try {
			if(user.getUserName() != null && !user.getUserName().isEmpty())
			userPresentation.insertUser(user);
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private User selectUser(String userName){
		try{
			
			return userPresentation.selectUser(userName);
		} catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}


	public String getPSOExecutionTimes() {
		return recomad.getLastTenTimesForPSORecomandation();
	}

	public String getClonalExecutionTImes() {
		return recomad.getLastTenTimesForClonalRecomandation();
	}
}
